package com.shuke.sk_worm.service.Impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuke.sk_worm.Mapper.SkAccountInfoMapper;
import com.shuke.sk_worm.Mapper.SkShopInfoMapper;
import com.shuke.sk_worm.domain.SkAccountInfo;
import com.shuke.sk_worm.domain.SkAccounts;
import com.shuke.sk_worm.domain.SkShopInfo;
import com.shuke.sk_worm.service.SkShopInfoService;
import com.shuke.sk_worm.vo.ShopInfoListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SkShopInfoServiceImpl extends ServiceImpl<SkShopInfoMapper, SkShopInfo>
    implements SkShopInfoService {

    @Autowired
    private SkShopInfoMapper skShopInfoMapper;

    @Autowired
    private SkAccountInfoMapper accountInfoMapper;

    public List<ShopInfoListVo> listShop(Integer platform){
        List<ShopInfoListVo> shopInfoListVos = skShopInfoMapper.findShopInfoList(platform);
        if (shopInfoListVos.isEmpty()){
            return null;
        }

        return shopInfoList(shopInfoListVos);
    }

    public List<ShopInfoListVo> listServiceShop(Integer platform, Integer deptId){
        List<ShopInfoListVo> shopInfoListVos = skShopInfoMapper.listServiceShop(platform, deptId);
        if (shopInfoListVos.isEmpty()){
            return null;
        }

        return shopInfoList(shopInfoListVos);
    }

    public List<ShopInfoListVo> listShopByIds(Integer platform, List<Long> shopIds){
        List<ShopInfoListVo> shopInfoListVos = skShopInfoMapper.listShopByIds(platform, shopIds);
        if (shopInfoListVos.isEmpty()){
            return null;
        }

        return shopInfoList(shopInfoListVos);
    }

    public List<ShopInfoListVo> listShopByIsAbnormal(Integer platform, Integer isAbnormal){
        List<ShopInfoListVo> shopInfoListVos = skShopInfoMapper.listShopByIsAbnormal(platform, isAbnormal);
        if (shopInfoListVos.isEmpty()){
            return null;
        }

        return shopInfoList(shopInfoListVos);
    }

    public List<ShopInfoListVo> listShopByLoginFail(Integer platform){
        List<ShopInfoListVo> shopInfoListVos = skShopInfoMapper.listShopByLoginFail(platform);
        if (shopInfoListVos.isEmpty()){
            return null;
        }

        return shopInfoList(shopInfoListVos);
    }


    public List<ShopInfoListVo> listShopByUpdateTime(Integer platform){
        List<ShopInfoListVo> shopInfoListVos = skShopInfoMapper.listShopByUpdateTime(platform, DateUtil.beginOfDay(new Date()));
        if (shopInfoListVos.isEmpty()){
            return null;
        }

        return shopInfoList(shopInfoListVos);
    }

    public List<ShopInfoListVo> shopInfoList(List<ShopInfoListVo> shopInfoListVos){

        List<Long> shopIds = new ArrayList<>();  //店铺ID
        for (int i = 0; i<shopInfoListVos.size(); i++){
            shopIds.add(shopInfoListVos.get(i).getId());
            shopInfoListVos.get(i).setName(shopInfoListVos.get(i).getName().replaceAll("\\t|\r|\n", "").trim());
            shopInfoListVos.get(i).setSorts(i);
        }

        List<SkAccountInfo> accountInfos = accountInfoMapper.findAccountByShopId(shopIds);

        accountInfos = accountInfos.stream().map(vo->{
            if (!StringUtils.isEmpty(vo.getShopAccount())){
                vo.setShopAccount(vo.getShopAccount().replaceAll("\\t|\r|\n", "").trim());
            }
            if (!StringUtils.isEmpty(vo.getUserName())){
                vo.setUserName(vo.getUserName().replaceAll("\\t|\r|\n", "").trim());
            }

            return vo;
        }).collect(Collectors.toList());

        Map<Long, List<SkAccountInfo>> map = accountInfos.stream().collect(Collectors.groupingBy(SkAccountInfo::getShopId));

        for(int i = 0; i < shopInfoListVos.size(); i++){
            Long shopId = shopInfoListVos.get(i).getId();
            if (map.containsKey(shopId)){
                shopInfoListVos.get(i).setAccountInfos(map.get(shopId));
            } else{
                //没有子账号直接删除，避免店铺登录，节约时间
                shopInfoListVos.remove(i);
            }
        }

        return shopInfoListVos;
    }

    public List<ShopInfoListVo> listNotTokenShop(Integer platform){
        return skShopInfoMapper.listNotTokenShop(platform);
    }

    public List<ShopInfoListVo> listCookieShop(Integer platform){
        List<ShopInfoListVo> shopInfoListVos = skShopInfoMapper.findShopInfoListByCookie(platform, 54 * 3600);
        if (shopInfoListVos.isEmpty()){
            return null;
        }
        for (int i = 0; i<shopInfoListVos.size(); i++){
            shopInfoListVos.get(i).setSorts(i);
        }
        return shopInfoListVos;
    }

    public Boolean updateAbnormal(Long shopId){
        return skShopInfoMapper.updateAbnormal(shopId);
    }

    public Boolean updateAbnormalTime(Long shopId, Date errorTime){
        return skShopInfoMapper.updateAbnormalTime(shopId, errorTime);
    }

    public Boolean updateNotAbnormalByShopId(Long shopId){
        return skShopInfoMapper.updateNotAbnormalByShopId(shopId);
    }

    public ShopInfoListVo getShopByShopId(Long shopId){
        ShopInfoListVo shopInfoListVo = new ShopInfoListVo();

        SkShopInfo shop = this.getById(shopId);
        if (shop == null){
            return null;
        }

        List<Long> shopIds = new ArrayList<>();
        shopIds.add(shopId);

        accountInfoMapper.findAccountByShopId(shopIds);

        return shopInfoListVo;
    }

    public List<SkAccounts> findShopAccounts(Integer platform){
        return skShopInfoMapper.findShopAccounts(platform);
    }
}
